package com.acme.bankapp.apps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientApplication {

    private final int port;
    private final String serverIp;

    public ClientApplication(String serverIp, int port) {
        this.serverIp = serverIp;
        this.port = port;
    }

    public void start() {

        System.out.println("Client mode");
        startGeneratingRequests();

//
//        try (Socket kkSocket = new Socket(serverIp, port);
//             PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
//                     true);
//             BufferedReader in = new BufferedReader(new InputStreamReader(
//                     kkSocket.getInputStream()));) {
//
//            BufferedReader stdIn = new BufferedReader(new InputStreamReader(
//                    System.in));
//
//            String command = stdIn.readLine();
//            if (command.equals("go")) {
//                startGeneratingRequests(in, out);
//                return;
//            }
//
//            String fromServer;
//            String fromUser;
//
//            while ((fromServer = in.readLine()) != null) {
//                fromServer = fromServer.replace("\\n", "\n");
//                System.out.println("Server: " + fromServer);
//
//                fromUser = stdIn.readLine();
//                if (fromUser != null) {
//                    System.out.println("Client: " + fromUser);
//                    out.println(fromUser);
//                }
//            }
//        } catch (UnknownHostException e) {
//            System.err.println("Don't know about host " + serverIp);
//        } catch (IOException e) {
//            System.err.println("Couldn't get I/O for the connection to "
//                    + serverIp);
//        } catch (InterruptedException e) {
//            System.err.println(e.getMessage());
//            e.printStackTrace();
//        }

    }

    private void startGeneratingRequests() {
        try (Socket kkSocket = new Socket(serverIp, port);
             PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
                     true);
             BufferedReader in = new BufferedReader(new InputStreamReader(
                     kkSocket.getInputStream()))) {
            System.out.println(in.readLine());

            for (int i = 0; i < 15; i++) {
                addAndRemoveClients(out, in);
            }

            send(out, BankApplication.END_SESSION_MESSAGE);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void addAndRemoveClients(PrintWriter out, BufferedReader in) throws IOException, InterruptedException {
        for (int i = 0; i < 700; i++) {
            addClient(i, out, in);
        }
        removeAllClients(out, in);
    }

    private void addClient(int i, PrintWriter out, BufferedReader in) throws InterruptedException, IOException {
        String str = BankApplication.ADD_CLIENT + " client" + i + ":m";
        long t = System.currentTimeMillis();
        send(out, str);
        get(in);
        System.out.println(System.currentTimeMillis() - t);
        Thread.sleep(20);
    }

    private void removeClient(int i, PrintWriter out, BufferedReader in) throws IOException, InterruptedException {
        String str = BankApplication.REMOVE_CLIENT + " client" + i;
        send(out, str);
        get(in);
        Thread.sleep(20);
    }

    private void removeAllClients(PrintWriter out, BufferedReader in) throws IOException {
        String str = BankApplication.REMOVE_ALL_CLIENTS;
        send(out, str);
        get(in);
    }

    private void send(PrintWriter out, String fromUser) {
        System.out.println("->" + fromUser);
        out.println(fromUser);
    }

    private void get(BufferedReader in) throws IOException {
        String str = in.readLine();
        if (str == null) {
            return;
        }
        str = str.replace("\\n", "\n");
        System.out.println("<-" + str);
    }

}
